package com.dy.system.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.dy.common.utils.DateUtils;
import com.dy.system.domain.FreshWarningMsgType;
import com.dy.system.mapper.FreshWarningMsgTypeMapper;
import com.dy.system.service.IFreshWarningMsgTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 告警消息类型Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-15
 */
@Service
public class FreshWarningMsgTypeServiceImpl implements IFreshWarningMsgTypeService {
    @Autowired
    private FreshWarningMsgTypeMapper freshWarningMsgTypeMapper;

    private static final Logger logger = LoggerFactory.getLogger(FreshWarningMsgTypeServiceImpl.class);

    /**
     * 查询告警消息类型
     *
     * @param id 告警消息类型主键
     * @return 告警消息类型
     */
    @Override
    public FreshWarningMsgType selectFreshWarningMsgTypeById(Long id) {
        return freshWarningMsgTypeMapper.selectFreshWarningMsgTypeById(id);
    }

    /**
     * 查询告警消息类型列表
     *
     * @param freshWarningMsgType 告警消息类型
     * @return 告警消息类型
     */
    @Override
    public List<FreshWarningMsgType> selectFreshWarningMsgTypeList(FreshWarningMsgType freshWarningMsgType) {
        return freshWarningMsgTypeMapper.selectFreshWarningMsgTypeList(freshWarningMsgType);
    }

    /**
     * 新增告警消息类型
     *
     * @param freshWarningMsgType 告警消息类型
     * @return 结果
     */
    @Override
    public int insertFreshWarningMsgType(FreshWarningMsgType freshWarningMsgType) {
        freshWarningMsgType.setCreateTime(DateUtils.getNowDate());
        return freshWarningMsgTypeMapper.insertFreshWarningMsgType(freshWarningMsgType);
    }

    /**
     * 修改告警消息类型
     *
     * @param freshWarningMsgType 告警消息类型
     * @return 结果
     */
    @Override
    public int updateFreshWarningMsgType(FreshWarningMsgType freshWarningMsgType) {
        return freshWarningMsgTypeMapper.updateFreshWarningMsgType(freshWarningMsgType);
    }

    /**
     * 批量删除告警消息类型
     *
     * @param ids 需要删除的告警消息类型主键
     * @return 结果
     */
    @Override
    public int deleteFreshWarningMsgTypeByIds(Long[] ids) {
        return freshWarningMsgTypeMapper.deleteFreshWarningMsgTypeByIds(ids);
    }

    /**
     * 删除告警消息类型信息
     *
     * @param id 告警消息类型主键
     * @return 结果
     */
    @Override
    public int deleteFreshWarningMsgTypeById(Long id)
    {
        return freshWarningMsgTypeMapper.deleteFreshWarningMsgTypeById(id);
    }

    @Override
    public void saveOrUpdateMsgType(FreshWarningMsgType msgType) {

        FreshWarningMsgType warningMsgType = freshWarningMsgTypeMapper.selectWarningMsgByType(msgType);

        logger.info("msgType = {} and warningMsgType = {}", JSON.toJSONString(msgType),JSON.toJSONString(warningMsgType));
        if (warningMsgType == null){
            this.insertFreshWarningMsgType(msgType);
        }else {
            msgType.setId(warningMsgType.getId());
            this.updateFreshWarningMsgType(msgType);
        }
    }
}
